home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Modules / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  6.4 KB  |  250 lines  |  [TEXT/CWIE]

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Python interpreter main program */
  33.  
  34. #include "Python.h"
  35.  
  36.  
  37. /* Interface to getopt(): */
  38. extern int optind;
  39. extern char *optarg;
  40. extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
  41.  
  42.  
  43. /* Subroutines that live in their own file */
  44. extern char *Py_GetVersion();
  45. extern char *Py_GetCopyright();
  46.  
  47.  
  48. /* For Py_GetProgramName(); set by main() */
  49. static char *argv0;
  50.  
  51. /* For Py_GetArgcArgv(); set by main() */
  52. static char **orig_argv;
  53. static int  orig_argc;
  54.  
  55.  
  56. /* Short usage message (with %s for argv0) */
  57. static char *usage_line =
  58. "usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n";
  59.  
  60. /* Long usage message, split into parts < 512 bytes */
  61. static char *usage_top = "\n\
  62. Options and arguments (and corresponding environment variables):\n\
  63. -d     : debug output from parser (also PYTHONDEBUG=x)\n\
  64. -i     : inspect interactively after running script (also PYTHONINSPECT=x)\n\
  65. -s     : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\
  66. -u     : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
  67. -v     : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
  68. -c cmd : program passed in as string (terminates option list)\n\
  69. ";
  70. static char *usage_bot = "\
  71. file   : program read from script file\n\
  72. -      : program read from stdin (default; interactive mode if a tty)\n\
  73. arg ...: arguments passed to program in sys.argv[1:]\n\
  74. \n\
  75. Other environment variables:\n\
  76. PYTHONSTARTUP: file executed on interactive startup (no default)\n\
  77. PYTHONPATH   : colon-separated list of directories prefixed to the\n\
  78.                default module search path.  The result is sys.path.\n\
  79. ";
  80.  
  81.  
  82. /* Main program */
  83.  
  84. int
  85. main(argc, argv)
  86.     int argc;
  87.     char **argv;
  88. {
  89.     int c;
  90.     int sts;
  91.     char *command = NULL;
  92.     char *filename = NULL;
  93.     FILE *fp = stdin;
  94.     char *p;
  95.     int inspect = 0;
  96.     int unbuffered = 0;
  97.  
  98.     orig_argc = argc;    /* For Py_GetArgcArgv() */
  99.     orig_argv = argv;
  100.     argv0 = argv[0];    /* For Py_GetProgramName() */
  101.  
  102.     if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
  103.         Py_DebugFlag = 1;
  104.     if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
  105.         Py_SuppressPrintingFlag = 1;
  106.     if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
  107.         Py_VerboseFlag = 1;
  108.     if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
  109.         inspect = 1;
  110.     if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
  111.         unbuffered = 1;
  112.  
  113.     while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
  114.         if (c == 'c') {
  115.             /* -c is the last option; following arguments
  116.                that look like options are left for the
  117.                the command to interpret. */
  118.             command = malloc(strlen(optarg) + 2);
  119.             if (command == NULL)
  120.                 Py_FatalError(
  121.                    "not enough memory to copy -c argument");
  122.             strcpy(command, optarg);
  123.             strcat(command, "\n");
  124.             break;
  125.         }
  126.         
  127.         switch (c) {
  128.  
  129.         case 'd':
  130.             Py_DebugFlag++;
  131.             break;
  132.  
  133.         case 'i':
  134.             inspect++;
  135.             break;
  136.  
  137.         case 's':
  138.             Py_SuppressPrintingFlag++;
  139.             break;
  140.  
  141.         case 'u':
  142.             unbuffered++;
  143.             break;
  144.  
  145.         case 'v':
  146.             Py_VerboseFlag++;
  147.             break;
  148.  
  149.         /* This space reserved for other options */
  150.  
  151.         default:
  152.             fprintf(stderr, usage_line, argv[0]);
  153.             fprintf(stderr, usage_top);
  154.             fprintf(stderr, usage_bot);
  155.             exit(2);
  156.             /*NOTREACHED*/
  157.  
  158.         }
  159.     }
  160.  
  161.     if (unbuffered) {
  162. #ifndef MPW
  163.         setbuf(stdout, (char *)NULL);
  164.         setbuf(stderr, (char *)NULL);
  165. #else
  166.         /* On MPW (3.2) unbuffered seems to hang */
  167.         setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
  168.         setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
  169. #endif
  170.     }
  171.  
  172.     if (command == NULL && optind < argc &&
  173.         strcmp(argv[optind], "-") != 0)
  174.         filename = argv[optind];
  175.  
  176.     if (Py_VerboseFlag ||
  177.         command == NULL && filename == NULL && isatty((int)fileno(fp)))
  178.         fprintf(stderr, "Python %s\n%s\n",
  179.             Py_GetVersion(), Py_GetCopyright());
  180.     
  181.     if (filename != NULL) {
  182.         if ((fp = fopen(filename, "r")) == NULL) {
  183.             fprintf(stderr, "%s: can't open file '%s'\n",
  184.                 argv[0], filename);
  185.             exit(2);
  186.         }
  187.     }
  188.     
  189.     Py_Initialize();
  190.     
  191.     if (command != NULL) {
  192.         /* Backup optind and force sys.argv[0] = '-c' */
  193.         optind--;
  194.         argv[optind] = "-c";
  195.     }
  196.  
  197.     PySys_SetArgv(argc-optind, argv+optind);
  198.  
  199.     if (command) {
  200.         sts = PyRun_SimpleString(command) != 0;
  201.     }
  202.     else {
  203.         if (filename == NULL && isatty((int)fileno(fp))) {
  204.             char *startup = getenv("PYTHONSTARTUP");
  205.             if (startup != NULL && startup[0] != '\0') {
  206.                 FILE *fp = fopen(startup, "r");
  207.                 if (fp != NULL) {
  208.                     (void) PyRun_SimpleFile(fp, startup);
  209.                     PyErr_Clear();
  210.                     fclose(fp);
  211.                 }
  212.             }
  213.         }
  214.         sts = PyRun_AnyFile(
  215.             fp, filename == NULL ? "<stdin>" : filename) != 0;
  216.         if (filename != NULL)
  217.             fclose(fp);
  218.     }
  219.  
  220.     if (inspect && isatty((int)fileno(stdin)) &&
  221.         (filename != NULL || command != NULL))
  222.         sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
  223.  
  224.     Py_Exit(sts);
  225.     /*NOTREACHED*/
  226. }
  227.  
  228.  
  229. /* Return the program name -- some code out there needs this
  230.    (currently _tkinter.c and importdl.c). */
  231.  
  232. char *
  233. Py_GetProgramName()
  234. {
  235.     return argv0;
  236. }
  237.  
  238.  
  239. /* Make the *original* argc/argv available to other modules.
  240.    This is rare, but it is needed by the secureware extension. */
  241.  
  242. void
  243. Py_GetArgcArgv(argc, argv)
  244.     int *argc;
  245.     char ***argv;
  246. {
  247.     *argc = orig_argc;
  248.     *argv = orig_argv;
  249. }
  250.